package com.njth.servlet.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.njth.beans.Category;
import com.njth.dao.CategoryDAO;
import com.njth.dao.DaoConfig;
import com.njth.util.Config;
import com.njth.util.FileAssistant;
import com.njth.util.ScaleImage;

public class AddCategory extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 518755239686345451L;

	/**
	 * Constructor of the object.
	 */
	public AddCategory() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		CategoryDAO dao=(CategoryDAO) DaoConfig.getDao(CategoryDAO.class);
		
		String path=Config.getConfigProperty("uploadImagePath");
		File uploadAbsolutDirectory=new File(Config.getContenAbsolutPath(),path);
		
		Map<String,Object> map=FileAssistant.fileUpload(request, uploadAbsolutDirectory);
		
		String name=(String)map.get("categoryname");
		String funcvar=(String)map.get("categoryfunc");
		Category c=new Category();
		c.setName(name.trim());
		try {
			int funcid=Integer.parseInt(funcvar);
			c.setCategoryfuncid(funcid);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		File cateImg=(File)map.get("file1");
		String cName=cateImg.getName();
		String[] strs = cName.split("\\.");
        String tName = "";
        for (int j= 0; j < strs.length; j++) {
            if (j == strs.length - 1) {
                tName = tName + "_t." + strs[j];
            } else {
                tName = tName + strs[j];
            }
        }
      
		c.setCategorypic(cName);
		ScaleImage si=new ScaleImage();
		try {
			String var=Config.getConfigProperty("imgThumbnailHeight");
			int h=Integer.parseInt(var);
			var=Config.getConfigProperty("imgThumbnailWidth");
			int w=Integer.parseInt(var);
			si.saveImageAsJpg(cateImg.getAbsolutePath(), uploadAbsolutDirectory.getAbsolutePath()+File.separatorChar+tName, w, h);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		c.setPicThumbnail(tName);
		request.setAttribute("newCate", c);
		dao.insert(c);
		
		//产品类别插入成功，跳转到指定页面
		response.sendRedirect(request.getContextPath()+"/com/njth/jsp/addCategory.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
